package com.duoketech.controller.web.roomorder;

import com.duoketech.controller.BaseController;
import com.duoketech.controller.web.ICRUD;
import com.duoketech.exception.ValidateException;
import com.duoketech.model.room.UnitRoom;
import com.duoketech.model.roomorder.RoomOrder;
import com.duoketech.model.unit.Unit;
import com.duoketech.service.web.room.UnitRoomService;
import com.duoketech.service.web.roomorder.RoomOrderService;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;

public class RoomOrderController extends BaseController implements ICRUD {

    @Override
    public void listAll() {
        int pageNum = getParaToInt("pageNum",1);
        int pageSize = getParaToInt("pageSize",20);
        Kv kv = Kv.by("memberId",getShopMemberId());
        Page<RoomOrder> result = (Page<RoomOrder>) RoomOrderService.ME.pageAllRecord(getModel(RoomOrder.class),pageNum,pageSize,kv);
        for(RoomOrder o:result.getList()){
            o.set(RoomOrder.STATUS,o.status());
            o.set(RoomOrder.START_TIME,o.startTime());
            o.set(RoomOrder.END_TIME,o.endTime());
            o.set(RoomOrder.TOTAL_PRICE,o.totalPrice());
        }
        renderObject(result);
    }

    @Override
    public void queryById() {
        int id = getParaToInt("id",0);
        try {
            RoomOrder order = (RoomOrder) RoomOrderService.ME.queryBy(id,getModel(RoomOrder.class),Kv.create());
            int obj_id = order.getInt(RoomOrder.OBJ_ID);
            UnitRoom room = UnitRoom.dao.findById(obj_id);
            setAttr("room",room);
            setAttr("roomOrder",order);
            render("edit-"+viewModel()+".html");
            } catch (ValidateException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doAdd() {
        try {
            RoomOrderService.ME.doAdd(getModel(RoomOrder.class),Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }

    }

    @Override
    public void doUpdate() {
        try {
            RoomOrderService.ME.update(getModel(RoomOrder.class),Kv.create());
            renderSuccess("操作成功");
            } catch (ValidateException e) {
            renderError(e.getMessage());
        }
    }

    @Override
    public void doDelete() {
        int id = getParaToInt("id",0);
        try {
        RoomOrderService.ME.deleteBy(id,getModel(RoomOrder.class),Kv.create());
        renderSuccess("操作成功");
        } catch (ValidateException e) {
        renderError(e.getMessage());
        }
    }
  }